<template>
  <q-dialog v-model="show" persistent>
    <q-card v-drag class="dialog-content" style="min-width: 400px; max-height: 500px">
      <q-card-section class="dialog-title bg-primary" >
        <div class="text-center text-white dialog-title">
          <span class="font-size-16">
            编辑原因
          </span>
        </div>
      </q-card-section>
      <div class="form-inline" style="padding: 20px 20px 0;display: block">
        <div class="q-field row no-wrap items-start q-input q-field--outlined q-field--float q-field--dense q-field--with-bottom items-center" v-if="groupName">
          <div class="q-field__before q-field__marginal row no-wrap items-center">
            <label class="text-right name">
              <span>原因类型:</span>
            </label>
          </div>
          <div>{{groupName}}</div>
        </div>
        <q-input
          v-model.trim="typeCode"
          dense
          outlined
          :rules="[
                    val => val && val.length > 0 || '请输入原因代码',
                    val => val && val.length < 20  || '原因代码过长',
                    val => val && !/[\u4E00-\u9FA5]/g.test(val) || '请输入英文、数字或符号'
                  ]"
        >
          <template v-slot:before>
            <label class="text-right name">
              <span class="text-red">*</span>
              <span>原因代码:</span>
            </label>
          </template>
        </q-input>
<!--        <q-input-->
<!--          v-model.trim="typeName"-->
<!--          dense-->
<!--          outlined-->
<!--          :rules="[-->
<!--            val => val && val.length > 0 || '请输入名称'-->
<!--          ]"-->
<!--        >-->
        <q-input
          v-model.trim="typeName"
          type="textarea" counter autogrow maxlength="50"
          outlined
          :rules="[val => val && val.length > 0 || '请输入原因名称']"
          class="textarea"
        >
          <template v-slot:before>
            <label class="text-right name">
              <span class="text-red">*</span>
              <span>原因名称:</span>
            </label>
          </template>
        </q-input>
      </div>
      <!-- 底部按钮 -->
      <q-separator />
      <div class="dialog-footer text-center margin-t-10">
        <q-btn outline label="取消" color="primary" @click="$emit('close')" class="q-mr-md"/>
        <q-btn unelevated label="确认" color="primary" @click="submit" :disable="!typeCode || !typeName"/>
      </div>
    </q-card>
  </q-dialog>
</template>

<script>
export default {
  name: 'edit-reason-item',
  props: {
    show: {
      type: Boolean,
      default: false,
      required: true
    },
    code: {
      default: '',
      required: false
    },
    label: {
      default: '',
      required: false
    },
    groupName: {
      default: '',
      required: true
    }
  },
  data () {
    return {
      typeName: null,
      typeCode: null
    }
  },
  methods: {
    submit () {
      if (/[\u4E00-\u9FA5]/g.test(this.typeCode)) {

      } else {
        const params = {
          typeCode: this.typeCode,
          typeName: this.typeName
        }
        this.$emit('editReason', params)
      }
    }
  },
  watch: {
    show: function () {
      this.typeName = this.label
      this.typeCode = this.code
    }
  }
}
</script>

<style scoped lang="stylus">

</style>
